﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            //Tast1();
            //Tast2();
            //Tast3();
            //Tast4();
            //Tast5();
            //Tast6();
            Tast7();
        }
        //正方形
        static void Tast1()
        {
            string[,] arr = new string[5,5];
            for (int i = 0; i < arr.GetLength(0); i++)
            {
                for (int j = 0; j < arr.GetLength(1); j++)
                {
                    arr[i, j] = "* ";
                }
            }
            for (int i = 0; i < arr.GetLength(0); i++)
            {
                for (int j = 0; j < arr.GetLength(1); j++)
                {
                    Console.Write(arr[i,j]);
                }
                Console.WriteLine();
            }
        }
        //九九乘法表
        static void Tast3()
        {
            string[,] arr = new string[9, 9];
            for (int i = 0; i < arr.GetLength(0); i++)
            {
                for (int j = 0; j <i; j++)
                {
                    arr[i, j] = (j+1) + " * " + (i+1) + " = " + (i+1) * (j+1)+"  "; 
                }
            }
            for (int i = 0; i < arr.GetLength(0); i++)
            {
                for (int j = 0; j < arr.GetLength(1); j++)
                {
                    Console.Write(arr[i, j]);
                }
                Console.WriteLine();
            }

        }
        //菱形
        static void Tast4()
        {
            int x = 5, y = (x - 1) / 2;
            string[,] arr = new string[x, x];
            for (int i = 0; i <= y; i++)
            {
                for (int j = 0; j < arr.GetLength(1); j++)
                {
                    if (j>=y-i&&j<=y+i)
                    {
                        arr[i, j] = "* ";
                    }
                    else
                    {
                        arr[i, j] = "  ";
                    }
                }
            }
            for (int i = (x-1); i > y; i--)
            {
                for (int j = 0; j < arr.GetLength(1); j++)
                {
                    if (j >= i - y && j < x + y - i)
                    {
                        arr[i, j] = "* ";
                    }
                    else
                    {
                        arr[i, j] = "  ";
                    }
                }
            }
            for (int i = 0; i < arr.GetLength(0); i++)
            {
                for (int j = 0; j < arr.GetLength(1); j++)
                {
                    Console.Write(arr[i, j]);
                }
                Console.WriteLine();
            }

        }
        //杨辉三角
        static void Tast5()
    {
            int[][]arr = new int[10][];
            for (int i = 0; i < arr.GetLength(0); i++)
            {
                arr[i] = new int[i + 1];
            }
            for (int i = 0; i < arr.GetLength(0); i++)
            {
                for (int j = 0; j <=i; j++)
                {
                    if (i==0||j==0)
                    {
                        arr[i][j] = 1;
                    }
                    else
                    {
                        //arr[i, j] =Convert.ToString(int.Parse(arr[i - 1, j]) + int.Parse(arr[i - 1, j - 1]));
                        if ((i==2&&j==2)||i==j)
                        {
                            arr[i][j] = 1;
                        }
                        else
                        {
                            arr[i][j] = arr[i - 1][j] + arr[i - 1][j - 1];
                        }
                    }
                }
            }
            
            for (int i = 0; i < arr.GetLength(0); i++)
            {
                foreach (int item in arr[i])
                {
                    Console.Write(item+" ");
                }
                Console.WriteLine();
            }


        }
        //空心正方形
        static void Tast2()
        {
            string[,] arr = new string[5, 5];
            for (int i = 0; i < arr.GetLength(0); i++)
            {
                for (int j = 0; j < arr.GetLength(1); j++)
                {
                    if (i==1||j==1||i==4||j==4)
                    {
                        arr[i, j] = "* ";
                    }
                    else
                    {
                        arr[i, j] = "  ";
                    }
                }
            }
            for (int i = 0; i < arr.GetLength(0); i++)
            {
                for (int j = 0; j < arr.GetLength(1); j++)
                {
                    Console.Write(arr[i, j]);
                }
                Console.WriteLine();
                
            }
            
        }
        //数组练习1
        static void Tast6() 
        {
            int[] arr = { 1,2,4,5,6};
            Console.Write("数组中下标是偶数的元素有：");
            for (int i = 0; i < arr.GetLength(0); i++)
            {
                if (i % 2 == 0)
                {
                        Console.Write(arr[i]+" ");
                }
               
            }
        }
        //数组练习2
        static void Tast7() 
        {
            //如果一个数组保存元素是有序的（从大到小），
            //向这个数组中插入一个数，
            //使得插入后的数组元素仍然保持有序。
            int[] arr = { 10,7,4,3};
            int[] arr1 = new int[5];
            int temp = 0;
            Console.WriteLine("插入前的数组：");
            foreach (int item in arr)
            {
                Console.Write(item+" ");
            }
            for (int i = 0; i < arr.GetLength(0); i++)
            {
                arr1[i] = arr[i];
            }

            Console.WriteLine();
            Console.WriteLine("请输入一个数：");
            arr1[4] = int.Parse(Console.ReadLine());
            Console.WriteLine("插入后的数组：");
            foreach (int item in arr1)
            {
                Console.Write(item + " ");
            }
            for (int i = 0; i < arr1.GetLength(0)-1; i++)
            {
                for (int j = 0; j < arr1.GetLength(0)-1-i; j++)
                {
                    if (arr1[j]<arr1[j+1])
                    {
                        temp = arr1[j];
                        arr1[j] = arr1[j+1];
                        arr1[j+1] = temp;

                    }
                }
            }
            Console.WriteLine("插入后的数组：");
            foreach (int item in arr1)
            {
                Console.Write(item + " ");
            }
        }
    }
}
